<%@ page import="cn.medemede.model.UsersBean" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="cn.medemede.dao.UsersDao" %>
<%@ page import="java.sql.SQLException" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <script type="text/javascript" src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

    <!-- 可选的 Bootstrap 主题文件（一般不用引入） -->
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    <title>学生信息列表</title>
</head>
<body style="padding-top: 70px">
<%  request.setCharacterEncoding("utf-8");
    response.setContentType("text/html;charset=UTF-8");
    UsersBean user= (UsersBean) session.getAttribute("user");
    ArrayList<UsersBean> stulist= null;
    try {
        stulist = UsersDao.find2();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    session.setAttribute("stulist",stulist);
%>
<nav class="navbar navbar-default navbar-fixed-top">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
            </button>
            <a class="navbar-brand" href="index.jsp"><strong>学生信息管理系统</strong></a>
        </div>
        <div class="navbar-right">
            <p class="navbar-text">【<%=user.getCharacter()%>】 <a href="log_suc.jsp" class="navbar-link"><%=user.getZname()%></a></p>
            <button type="button" class="btn btn-default navbar-btn " onclick="check()">注销</button>
        </div>
    </div>
</nav>

<div class="page-header" style="width: 400px;text-align: center;margin: 0 auto">
    <h1>查看</h1>
</div>

<div>
    <div class="panel panel-default" style="width: 1200px;margin: 20px auto auto auto">
        <!-- Default panel contents -->
        <div class="panel-heading">详细信息</div>
        <div class="panel-body">
            <p>
            <ul>
            <li>只有“管理员”和“教师”身份可以查看此页面,“学生”身份会直接进入个人信息页面。</li>
            <li>“管理员”和“教师”可以修改学生信息,只有“管理员”才可删除用户。</li>
            </ul>
            </p>
        </div>

        <!-- Table -->
        <table class="table table-striped table-hover" style="text-align: center">
            <tr>
                <td><strong>#</strong></td> <td><strong>用户名</strong></td> <td><strong>姓名</strong></td>
                <td><strong>性别</strong></td> <td><strong>出生日期</strong></td> <td><strong>年龄</strong></td>
                <td><strong>联系方式</strong></td> <td><strong>地区</strong></td> <td><strong>Email</strong></td>
                <td><strong>爱好</strong></td><td><strong>操作</strong></td>
            </tr>
            <%
                for (int i=0; i<stulist.size();i++) {
            %>
            <tr id="<%=i%>">
                <td><%=i+1%></td>
                <td><%=stulist.get(i).getUsername()%>
                </td>
                <td><%=stulist.get(i).getZname()%>
                </td>
                <td><%=stulist.get(i).getSex()%>
                </td>
                <td><%=stulist.get(i).getBorth()%>
                </td>
                <td><%=stulist.get(i).getAge()%>
                </td>
                <td><%=stulist.get(i).getPhone()%>
                </td>
                <td><%=stulist.get(i).getZposition()%>
                </td>
                <td><%=stulist.get(i).getEmail()%>
                </td>
                <td><%=stulist.get(i).getFavorite()%>
                </td>
                <td>
                    <button type="button" class="btn btn-info" onclick="window.location.href='stuchange.jsp?stuid=<%=i%>'">修改</button>
                    &nbsp;&nbsp;
                    <button type="button" class="btn btn-warning" onclick="studel(<%=i%>)">删除</button>
                </td>
            </tr>
            <% }
            %>
        </table>
    </div>

</div>
<script>
    function check() {
        if (confirm("注销?")){
            $.post("/javaweb/JServlet",{
                    actionName:"clear"},
                function (data) {
                    if (data.search("clearSuc")!==-1){
                        window.location.href="index.jsp";
                    }else {
                        alert("对不起，注销失败！请重试。");
                    }
                }
            );
        }
    }

    function studel(stuid) {
        if (confirm("删除"+(stuid+1)+"号学生？")){
            $.post("/javaweb/JServlet",
                {
                    actionName:"studel",
                    stuid:stuid
                },
                function (data) {
                    if (data.search("delSuc")!==-1){
                        alert("删除成功！");
                        window.location.reload();
                    }else {
                        alert("删除失败！您或许无权删除。");
                    }
                }
            );
        }
    }
</script>
</body>
</html>
